<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="字典名称">
              <a-input v-model="queryParam.name" placeholder="请输入字典名称"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="字典代码">
              <a-input v-model="queryParam.type" placeholder="请输入字典代码"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-button type="primary"@click="() => {this.init()}">查询</a-button>
            <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div class="table-operator">
      <a-button type="primary" icon="plus" @click="$refs.dictAddOrUpdate.edit('','add')">新建</a-button>
    </div>
    <a-table
      :columns="columns"
      :dataSource="tableData"
      :loading="loading"
      :pagination="pagination"
      :rowKey="record => record.id"
      @change="handleTableChange"
    >
    </a-table>
    <dict-add-or-update
      ref="dictAddOrUpdate"
      @ok="handleOk"
    />
    <dict-item-drawer
      ref="dictItemDrawer"
    />
  </a-card>
</template>

<script>
import { fetchList, delObj } from '@api/sys/dict'
import DictAddOrUpdate from './dictAddOrUpdate'
import DictItemDrawer from './dictItemDrawer'

export default {
  name: 'Dict',
  components: {
    DictAddOrUpdate,
    DictItemDrawer
  },
  data () {
    return {
      loading: false,
      columns: [
        {
          title: '#',
          width: '10%',
          customRender: (text, record, index) => {
            return (
              <div>
                {index + 1}
              </div>
            )
          }
        },
        {
          title: 'ID',
          width: '10%',
          dataIndex: 'id'
        }, {
          title: '字典名称',
          width: '10%',
          dataIndex: 'name'
        }, {
          title: '字典类型',
          width: '10%',
          dataIndex: 'type'
        }, {
          title: '描述',
          dataIndex: 'remark'
        },
        {
          title: '操作',
          width: '350px',
          customRender: (text, record, index) => {
            return (
              <div>
                <a onClick={(e) => this.edit(record)}
                >修改</a>
                <a-divider type="vertical" />
                <a href="javascript:;"
                  onClick={(e) => this.showDictItem(record)}
                >字典配置</a>
                <a-divider type="vertical" />
                <a-popconfirm title="是否删除字典" onConfirm={(e) => this.delete(record)} okText="是" cancelText="否">
                  <a-icon slot="icon" type="question-circle-o" style="color: red" />
                  <a href="javascript:;" style="color: red">删除</a>
                </a-popconfirm>
              </div>
            )
          }
        }
      ],
      tableData: [],
      pagination: {
        pageSize: 10,
        current: 1,
        total: 0,
        showSizeChanger: true
      },
      pages: {
        size: 10,
        current: 1
      },
      queryParam: {
        name: '',
        type: ''
      }
    }
  },
  methods: {
    init () {
      this.loading = true
      fetchList({
        ...this.queryParam,
        ...this.pages
      }).then(res => {
        this.tableData = res.data.records
        this.pagination.total = res.data.total
        this.loading = false
      })
    },
    handleTableChange (pagination, filters, sorter) {
      this.pagination = pagination
      this.pages.size = pagination.pageSize
      this.pages.current = pagination.current
      this.init()
    },
    edit (record) {
      this.$refs.dictAddOrUpdate.edit(record.id, 'edit')
    },
    delete (record) {
      delObj(record.id).then(_ => {
        this.$message.info('删除成功')
        this.init()
      })
    },
    showDictItem (record) {
      this.$refs.dictItemDrawer.init(record)
    },
    handleOk () {
      this.init()
    }
  },
  created () {
    this.init()
  }
}
</script>

<style scoped>

</style>
